package cn.herlioner.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import cn.herlioner.dao.ObjectDAO;
import cn.herlioner.pojo.TSObject;
import cn.herlioner.pojo.Teacher;
import cn.herlioner.service.PageService;

public class ObjectDAOImpl implements ObjectDAO{

	@Override
	public void add(TSObject o) {
		SessionFactory sf = new Configuration().configure()
				.buildSessionFactory();
		Session s = sf.openSession();
		s.beginTransaction();
		
		s.save(o);
		
		s.getTransaction().commit();
		s.close();
		sf.close();
	}

	@Override
	public List<TSObject> get(Long objnum) {
		List<TSObject> result = new ArrayList();
        SessionFactory sf = new Configuration().configure()
                .buildSessionFactory();
        Session s = sf.openSession();
        try {
        	result =  s.createQuery("from TSObject o where objnum=?").setLong(0, objnum).list();
		} catch (Exception e) {
			System.out.println("没有取到值");
		}
        
        s.close();
        sf.close();
        return result;
	}

	@Override
	public void delete(Long objnum) {
        SessionFactory sf = new Configuration().configure()
                .buildSessionFactory();
        Session s = sf.openSession();
        s.beginTransaction();
 
        TSObject p = (TSObject) s.get(TSObject.class, objnum);
        if(p!=null) {s.delete(p);}
        
 
        s.getTransaction().commit();
        s.close();
        sf.close();
	}

	@Override
	public void update(TSObject o) {
        SessionFactory sf = new Configuration().configure()
                .buildSessionFactory();
        Session s = sf.openSession();
        s.beginTransaction();
 
        s.update(o);
 
        s.getTransaction().commit();
        s.close();
        sf.close();
	}

	@Override
	public List<TSObject> list() {
		List<TSObject> result = new ArrayList();
		 
        SessionFactory sf = new Configuration().configure()
                .buildSessionFactory();
        Session s = sf.openSession();
 
        Query q = s.createQuery("from TSObject o");
 
        result = q.list();
 
        s.close();
        sf.close();
        return result;
	}

	@Override
	public PageService PageSelect(int pageNum,int pageSize) {
		List<TSObject> Objlist = new ArrayList();
        SessionFactory sf = new Configuration().configure()
                .buildSessionFactory();
        Session s = sf.openSession();
        // 查询列表  
        SQLQuery q = (SQLQuery) s.createSQLQuery("select * FROM TSObject o ORDER BY o.id ASC")
        		.addEntity(TSObject.class)
        		.setFirstResult((pageNum - 1) * pageSize)
        		.setMaxResults(pageSize);
        Objlist = q.list();
        
        //查询总数
        Long count = (Long) s.createQuery(//  
                "SELECT COUNT(*) FROM TSObject o")//  
                .uniqueResult(); 
        
        s.close();
        sf.close();
        
        System.out.println("数据库时间");
        return new PageService(pageNum, pageSize, count.intValue(), Objlist);
	}

	public PageService PageSelect(int currentPagechange, int pageSize, int redi) {
		List<TSObject> Objlist = new ArrayList();
        SessionFactory sf = new Configuration().configure()
                .buildSessionFactory();
        Session s = sf.openSession();
        String hql = "FROM TSOBJECT as o where o.credits like :credits ORDER BY o.id ASC";
//        String hql="from User as user where user.id like '%"+id+"%'";  
        SQLQuery q = (SQLQuery) s.createSQLQuery(hql)
        		.addEntity(TSObject.class)
        		.setFirstResult((currentPagechange - 1) * pageSize)
        		.setMaxResults(pageSize);
        q.setLong("credits", redi);
        Objlist = q.list();
        
        //查询总数
        Long count = (Long) s.createQuery(//  
                "FROM TSOBJECT as o where o.credits like :credits")
                .setParameter("credits", "%"+redi+"%")//  
                .uniqueResult(); 
        
        s.close();
        sf.close();
        System.out.println(Objlist);
        return new PageService(currentPagechange, pageSize, count.intValue(), Objlist);
	}


	public PageService PageSelect(int currentPagechange, int pageSize,
			String objnameorteanameselect) {
		List<TSObject> Objlist = new ArrayList();
        SessionFactory sf = new Configuration().configure()
                .buildSessionFactory();
        Session s = sf.openSession();
        String hql ="FROM TSOBJECT as o where o.objname like :obj ORDER BY o.id ASC";
        SQLQuery q =(SQLQuery) s.createSQLQuery(hql)
        		.addEntity(TSObject.class)
        		.setFirstResult((currentPagechange - 1) * pageSize)
        		.setMaxResults(pageSize);
        q.setString("obj", objnameorteanameselect);
        Objlist = q.list();
        
        
        Long count = (Long) s.createQuery(//  
                "SELECT COUNT(*) FROM TSOBJECT o where o.objname like ?")
                .setParameter(0,  "%"+objnameorteanameselect+"%")//  
                .uniqueResult(); 
        
        
        s.close();
        sf.close();
		return new PageService(currentPagechange, pageSize, count.intValue(), Objlist);
	}



	

}
